<?php
namespace app\common\lib;

use app\common\model\AdminAuth;
use app\common\model\AdminGroup;
use app\common\model\Admin;

class ManageAuth
{

	private $groupId;
	private $model;
	private $rule;

	public function __construct(){
		$this->model = new Admin;
	}

	public function checkUid($uid){
		$this->groupId = getUserGroupId($uid);
		$this->rule = strtolower($rule);
		return $this->check();
	}

	public function checkGroupId($groupId,$rule){
		$this->groupId = $groupId;
		$this->rule = strtolower($rule);
		return $this->check();
	}

	/**
	 * 获取用户组ID
	 * @return [type] [description]
	 */
	private function getUserGroupId($uid){
		return $this->model->where(['id'=>$uid])->value('group_id');
	}
	/**
	 * 获取用户组权限
	 * @return [type] [description]
	 */
	private function getGroupAuth(){
		$groupId = $this->groupId;
		$result = AdminGroup::adminGroupData($groupId);
		$authes = unserialize($result['group_auth']);
		return $authes;
	}
	/**
	 * 根据规则获取规则id
	 * @param  [type] $rule [description]
	 * @return [type]       [description]
	 */
	private function getRuleId(){
		$auth = AdminAuth::authByAuthPath($this->rule);
		return $auth['id'];
	}

	private function check(){
		$groupAuthes = $this->getGroupAuth();
		$ruleId = $this->getRuleId();

		if(!in_array($ruleId,$groupAuthes)){
			return false;
		}else{
			return true;
		}
	}

}